global root_dir = "`1'"
include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/Table_A43_macrosimulation_regression.log, replace name(tabl)

capture noi {

	global d = "`1'"
	do ${d}/code/macrosim/simulation_config.do "`1'/" $bvers


	use ${d}/datasets/macrosim/BvD_year_div_${ln_vers}_pauto95_${chosen_spec}.dta, clear 

	* Run regressions
	estimates clear
	foreach division in auto95 pauto95 {
		global eq ""
		foreach var of global spec {
			global eq "${eq} `var'"
		}
		di "${eq}"
		clonevar `division' = depvar
		sort firm_division year
		ppmlhdfe F2.`division' ${eq}  if year>=1995 & division == "`division'":division, absorb(${absorb}) vce(cluster lse_id) d
		estadd local iy "\yes"
		estadd local f "\yes"
		estadd local cy "\no"
		estadd local obs "{\num{`e(N)'}}"
		estadd local has_stockother "\yes"
		estadd local has_spilloversother "\yes"
		estadd local has_spilloversothersq "\yes"
		estadd local has_gdpgap "\yes"
		estadd local firms "{\num{`e(N_clust)'}}"
		estimates store est_`division'
		preserve
			keep if e(sample)
			keep BvD
			duplicates drop BvD, force
			save ${d}/datasets/macrosim/BvD_sample_`division'_${chosen_spec}_${FE}.dta, replace
		restore
		drop `division'
	}

	qui do ${code_dir}/config/tabletools.do
	setlabels

	dhoztab * using ${tab_dir}/appendix/Table_A43_macrosimulation_regression_${chosen_spec}_${FE}_${ln_vers}_pauto95.tex, b(%10.2f) se(%10.2f) nogaps replace numbers nonotes nolines showdepvarinfo nomtitles sfmt(a1) scalars("has_gdpgap \noalign{\medskip} GDP gap" "has_stockother Non-machinery stock" "has_spilloversother Non-machinery spillovers" "has_spilloversothersq Non-machinery spillovers squared" "f \noalign{\medskip} Firm fixed effects" "iy Industry \stimes year fixed effects" "cy Country \stimes year fixed effects" "obs \noalign{\medskip} Observations"  "firms Number of firms") label drop(*zero _cons *other GDPGAP*) noobs rename(LSW_foreign LSW HSW_foreign HSW GDPgap_foreign GDPGAP VAEMP_foreign VAEMP GDPPC_foreign GDPPC) ///
		order(ISP stockauto95 stockpauto95 spilloversauto95 spilloverspauto95 sqspilloversauto95 sqspilloverspauto95) ///
		depvar(none) rawaddheader({Dependent variable} &\multicolumn{1}{c}{Auto95}&\multicolumn{1}{c}{Pauto95} \\ \cmidrule(lr){2-2}\cmidrule(lr){3-3}) ///
		notes("This table shows regressions of automation (column 1) and non-automation machinery innovations (column 2) on the inverse skill-premium, the GDP gap, and firm-level stock and spillover variables. We consider automation, non-automation, and non-machinery stocks and spillovers separately and include squared spillovers. Stocks and spillovers are computed as log(1+). The regressions include firm and year-industry fixed effects. Standard errors are clustered at the firm-level and reported in parentheses.")


}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}